﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LogicAndDB;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace AccountManagement.code
{
    public class LogicHandler
    {
        /// <summary>
        /// TODO:未完成
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        static public CUser.RegisterResult Register(string time,CUser user)
        {

            string subject = "注册账号";
            String reason = "申请用户";
            String strEmail = "注册账户, 请使用以下链接进行注册";
            var key = System.Guid.NewGuid().ToString("N");
            string httpAuthority = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
            Uri uri = new Uri(httpAuthority + "/ActivateMial.aspx");
            string encodedMail = HttpUtility.UrlEncode(user.Mail);
            string encodedKey = HttpUtility.UrlEncode(key);
            string encodedTime = HttpUtility.UrlEncode(time);

            uri = new Uri(uri,"/ActivateMial.aspx?time="+encodedTime + "&email=" + encodedMail + "&key=" + encodedKey );
           
            if (!user.IsDataComplete())
                return CUser.RegisterResult.Failed;

            if (CUser.IsEmailDuplicated(user.Mail))
            {
                return CUser.RegisterResult.EmailAlreadyRegister;
            }
    
            if (!SendEmail(reason, subject, strEmail, uri.ToString(),user))
                return CUser.RegisterResult.Failed;

            if (CUser.AddNewUser(user))
            {
                return CUser.RegisterResult.Success;
            }
            else
            {
                return CUser.RegisterResult.Failed;
            }
            
        }

 
        static private bool SendEmail(string reason, string subject, string strRmail, string url,CUser toWhom)
        {
            string Str1 = "您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) " + reason + ".假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员";
            string Str3 = "(如果无法点击该URL链接地址,请将它复制并粘帖到浏览器的地址输入框,然后单击回车即可.该链接使用后将立即失效.)";
            string Str4 = " 注意:请您在收到邮件1个小时内使用,否则该链接将会失效.";
            string Str5 = "我们将一如既往、热忱的为您服务!";

            string to = toWhom.Mail ;
            string from = "865037188@qq.com";
            try
            {

                MailMessage message = new MailMessage(from, to);
                message.SubjectEncoding = System.Text.Encoding.Default;
                message.BodyEncoding = System.Text.Encoding.Default;
                message.Subject = "" + subject + "";
                message.Body = "您好!" + "<br>" + Str1 + "<br>" + strRmail + "<br>" + "<a href='" + url + "'>" + url + "</a>" + "<br>" + Str3 + "<br>" + Str4 + "<br>" + Str5;
                message.IsBodyHtml = true;

                //TODO:必须改掉，用配置中的。
                SmtpClient client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Host = "smtp.qq.com";
                client.Credentials = new NetworkCredential("865037188", "3X0Y6N1989");
                
                try
                {
                    client.Send(message);
                    return true;
                }
                catch(Exception ex)
                {
                    Logger.Write(ex);
                    return false;
                }
            }
            catch(Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }
    }
}